package com.ruoyi.web.controller.marketing;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.marketing.service.IMarketingAwardService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.marketing.domain.MarketingTask;
import com.ruoyi.marketing.service.IMarketingTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 营销任务Controller
 *
 * @author hao
 * @date 2023-01-30
 */
@RestController
@RequestMapping("/marketing/task")
public class MarketingTaskController extends BaseController {
    @Autowired
    private IMarketingTaskService marketingTaskService;

    @Autowired
    private IMarketingAwardService marketingAwardService;

    /**
     * 查询营销任务列表
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(MarketingTask marketingTask) {
        startPage();
        List<MarketingTask> list = marketingTaskService.selectMarketingTaskList(marketingTask);
        return getDataTable(list);
    }

    /**
     * 导出营销任务列表
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:export')")
    @Log(title = "营销任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MarketingTask marketingTask) {
        List<MarketingTask> list = marketingTaskService.selectMarketingTaskList(marketingTask);
        ExcelUtil<MarketingTask> util = new ExcelUtil<MarketingTask>(MarketingTask.class);
        util.exportExcel(response, list, "营销任务数据");
    }

    /**
     * 获取营销任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        return AjaxResult.success(marketingTaskService.getById(taskId));
    }

    /**
     * 新增营销任务
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:add')")
    @Log(title = "营销任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MarketingTask marketingTask) {
        return toAjax(marketingTaskService.save(marketingTask));
    }

    /**
     * 修改营销任务
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:edit')")
    @Log(title = "营销任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MarketingTask marketingTask) {
        return toAjax(marketingTaskService.updateById(marketingTask));
    }

    /**
     * 删除营销任务
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:remove')")
    @Log(title = "营销任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return toAjax(marketingTaskService.removeBatchByIds(Arrays.asList(taskIds)));
    }


    /**
     * //查询待做任务列表接口（返回符合条件的待做任务）
     * 获取当前用户所对应的活动任务列表
     *
     * @param activityId 活动编号
     * @return 列表
     */
    @GetMapping
    public AjaxResult getByActivity(@RequestParam Long activityId) {
        List<MarketingTask> list = marketingTaskService.getActivityById(activityId);
        return AjaxResult.success(list);
    }

    //完成任务接口（验证用户是否可以参加任务，增加参与任务记录，
    //根据任务规则决定是否可以完成任务，可以完成则直接更新状态为已完成）
    @PostMapping("/getActivity")
    public AjaxResult getActivity(Long userId, Long taskId) {
        marketingTaskService.getTaskById(userId, taskId);
        return AjaxResult.success();
    }


    /**
     * 抽奖
     * @param activityId
     * @return
     */
    @PostMapping("/reward/{marketId}")
    public AjaxResult reward(@PathVariable("marketId") String activityId) {
        marketingAwardService.getAward(activityId);
        return AjaxResult.success();
    }


}
